<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="本文作者为: SylvanasSun.转载请务必将下面这段话置于文章开头处(保留超链接).本文转发自SylvanasSun Blog,原文链接: https:&#x2F;&#x2F;sylvanassun.github.io&#x2F;2017&#x2F;07&#x2F;25&#x2F;2017-07-25-Graph_WeightedUndirectedGraph&#x2F;  加权无向图 所谓加权图,即每条边上都有着对应的权重,这个权重是正数也可以是负数,也不">
<meta property="og:type" content="article">
<meta property="og:title" content="图的那点事儿(3)-加权无向图">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="本文作者为: SylvanasSun.转载请务必将下面这段话置于文章开头处(保留超链接).本文转发自SylvanasSun Blog,原文链接: https:&#x2F;&#x2F;sylvanassun.github.io&#x2F;2017&#x2F;07&#x2F;25&#x2F;2017-07-25-Graph_WeightedUndirectedGraph&#x2F;  加权无向图 所谓加权图,即每条边上都有着对应的权重,这个权重是正数也可以是负数,也不">
<meta property="og:locale">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/edge-weighted-graph-representation.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/mst.png">
<meta property="og:image" content="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Multiple_minimum_spanning_trees.svg/316px-Multiple_minimum_spanning_trees.svg.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/cut-property.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/mst-greedy.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/prim.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/prim-lazy.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/prim-eager.png">
<meta property="og:image" content="http://algs4.cs.princeton.edu/43mst/images/kruskal.png">
<meta property="article:published_time" content="2017-07-25T10:00:00.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="2017">
<meta property="article:tag" content="Algorithms">
<meta property="article:tag" content="Graph">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://algs4.cs.princeton.edu/43mst/images/edge-weighted-graph-representation.png">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>图的那点事儿(3)-加权无向图 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8A%A0%E6%9D%83%E6%97%A0%E5%90%91%E5%9B%BE"><span class="nav-number">1.</span> <span class="nav-text">加权无向图</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Edge%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.1.</span> <span class="nav-text">Edge的实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8A%A0%E6%9D%83%E6%97%A0%E5%90%91%E5%9B%BE%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="nav-number">1.2.</span> <span class="nav-text">加权无向图的实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%80%E5%B0%8F%E7%94%9F%E6%88%90%E6%A0%91"><span class="nav-number">2.</span> <span class="nav-text">最小生成树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%87%E5%88%86%E5%AE%9A%E7%90%86"><span class="nav-number">3.</span> <span class="nav-text">切分定理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Prim%E7%AE%97%E6%B3%95"><span class="nav-number">4.</span> <span class="nav-text">Prim算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BB%B6%E6%97%B6%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.1.</span> <span class="nav-text">延时实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8D%B3%E6%97%B6%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.2.</span> <span class="nav-text">即时实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Kruskal%E7%AE%97%E6%B3%95"><span class="nav-number">5.</span> <span class="nav-text">Kruskal算法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%8EPrim%E7%AE%97%E6%B3%95%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">5.1.</span> <span class="nav-text">与Prim算法的区别</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0"><span class="nav-number">5.2.</span> <span class="nav-text">实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%80%A7%E8%83%BD%E6%AF%94%E8%BE%83"><span class="nav-number">5.3.</span> <span class="nav-text">性能比较</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="nav-number">6.</span> <span class="nav-text">参考文献</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E7%9A%84%E9%82%A3%E7%82%B9%E4%BA%8B%E5%84%BF"><span class="nav-number">7.</span> <span class="nav-text">图的那点事儿</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          图的那点事儿(3)-加权无向图
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2017-07-25 18:00:00" itemprop="dateCreated datePublished" datetime="2017-07-25T18:00:00+08:00">2017-07-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/Algorithms/" itemprop="url" rel="index"><span itemprop="name">Algorithms</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/Algorithms/Graph/" itemprop="url" rel="index"><span itemprop="name">Graph</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>本文作者为: <a target="_blank" rel="noopener" href="https://github.com/SylvanasSun">SylvanasSun</a>.转载请务必将下面这段话置于文章开头处(保留超链接).<br>本文转发自<a target="_blank" rel="noopener" href="https://sylvanassun.github.io/">SylvanasSun Blog</a>,原文链接: <a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/">https://sylvanassun.github.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/</a></p>
</blockquote>
<h3 id="加权无向图"><a href="#加权无向图" class="headerlink" title="加权无向图"></a>加权无向图</h3><hr>
<p>所谓<code>加权图</code>,即每条<code>边</code>上都有着对应的<code>权重</code>,这个<code>权重</code>是正数也可以是负数,也不一定会和距离成正比.<code>加权无向图</code>的表示方法只需要对<code>无向图</code>的实现进行一下扩展.</p>
<ul>
<li>在使用<code>邻接矩阵</code>的方法中,可以用<code>边</code>的<code>权重</code>代替布尔值来作为矩阵的元素.</li>
</ul>
<ul>
<li>在使用<code>邻接表</code> 的方法中,可以在<code>链表</code>的<code>节点</code>中添加一个权重域.</li>
</ul>
<ul>
<li>在使用<code>邻接表</code>的方法中,将<code>边</code>抽象为一个<code>Edge</code>类,它包含了相连的两个<code>顶点</code>和它们的<code>权重</code>,<code>链表</code>中的每个元素都是一个<code>Edge</code>.</li>
</ul>
<p>我们使用第三种方法来实现<code>加权无向图</code>,它的数据表示如下图:</p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/edge-weighted-graph-representation.png" alt="加权无向图的表示"></p>
<h4 id="Edge的实现"><a href="#Edge的实现" class="headerlink" title="Edge的实现"></a>Edge的实现</h4><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Edge</span> <span class="keyword">implements</span> <span class="title">Comparable</span>&lt;<span class="title">Edge</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> v;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> w;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">double</span> weight;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Edge</span><span class="params">(<span class="keyword">int</span> v, <span class="keyword">int</span> w, <span class="keyword">double</span> weight)</span> </span>&#123;</span><br><span class="line">        validateVertexes(v, w);</span><br><span class="line">        <span class="keyword">if</span> (Double.isNaN(weight)) <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Weight is NaN.&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.v = v;</span><br><span class="line">        <span class="keyword">this</span>.w = w;</span><br><span class="line">        <span class="keyword">this</span>.weight = weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateVertexes</span><span class="params">(<span class="keyword">int</span>... vertexes)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; vertexes.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (vertexes[i] &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(</span><br><span class="line">                        String.format(<span class="string">&quot;Vertex %d must be a nonnegative integer.&quot;</span>, vertexes[i]));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">weight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">either</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> v;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">other</span><span class="params">(<span class="keyword">int</span> vertex)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (vertex == v)</span><br><span class="line">            <span class="keyword">return</span> w;</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (vertex == w)</span><br><span class="line">            <span class="keyword">return</span> v;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Illegal endpoint.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(Edge that)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Double.compare(<span class="keyword">this</span>.weight, that.weight);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> String.format(<span class="string">&quot;%d-%d %.5f&quot;</span>, v, w, weight);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>Edge</code>类提供了<code>either()</code>与<code>other()</code>两个函数,在两个<code>顶点</code>都未知的情况下,可以调用<code>either()</code>获得<code>顶点v</code>,然后再调用<code>other(v)</code>来获得另一个<code>顶点</code>.</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/SylvanasSun/algs4-study/tree/master/src/main/java/chapter4_graphs/C4_3_MinimumSpanningTrees">本文中的所有完整代码点我查看</a></p>
</blockquote>
<h4 id="加权无向图的实现"><a href="#加权无向图的实现" class="headerlink" title="加权无向图的实现"></a>加权无向图的实现</h4><hr>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">EdgeWeightedGraph</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String NEWLINE = System.getProperty(<span class="string">&quot;line.separator&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> vertexes;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> edges;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Bag&lt;Edge&gt;[] adj;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">EdgeWeightedGraph</span><span class="params">(<span class="keyword">int</span> vertexes)</span> </span>&#123;</span><br><span class="line">        validateVertexes(vertexes);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.vertexes = vertexes;</span><br><span class="line">        <span class="keyword">this</span>.edges = <span class="number">0</span>;</span><br><span class="line">        adj = (Bag&lt;Edge&gt;[]) <span class="keyword">new</span> Bag[vertexes];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; vertexes; i++)</span><br><span class="line">            adj[i] = <span class="keyword">new</span> Bag&lt;&gt;();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">EdgeWeightedGraph</span><span class="params">(Scanner scanner)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>(scanner.nextInt());</span><br><span class="line">        <span class="keyword">int</span> edges = scanner.nextInt();</span><br><span class="line">        validateEdges(edges);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; edges; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> v = scanner.nextInt();</span><br><span class="line">            <span class="keyword">int</span> w = scanner.nextInt();</span><br><span class="line">            <span class="keyword">double</span> weight = scanner.nextDouble();</span><br><span class="line">            addEdge(<span class="keyword">new</span> Edge(v, w, weight));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">vertex</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> vertexes;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">edge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> edges;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addEdge</span><span class="params">(Edge e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> v = e.either();</span><br><span class="line">        <span class="keyword">int</span> w = e.other(v);</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        validateVertex(w);</span><br><span class="line">        adj[v].add(e);</span><br><span class="line">        adj[w].add(e);</span><br><span class="line">        edges++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;Edge&gt; <span class="title">adj</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> adj[v];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">degree</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        validateVertex(v);</span><br><span class="line">        <span class="keyword">return</span> adj[v].size();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;Edge&gt; <span class="title">edges</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Bag&lt;Edge&gt; list = <span class="keyword">new</span> Bag&lt;Edge&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertexes; v++) &#123;</span><br><span class="line">            <span class="keyword">int</span> selfLoops = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (Edge e : adj(v)) &#123;</span><br><span class="line">				<span class="comment">// 只添加一条边</span></span><br><span class="line">                <span class="keyword">if</span> (e.other(v) &gt; v) &#123;</span><br><span class="line">                    list.add(e);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 只添加一条自环的边</span></span><br><span class="line">                <span class="keyword">else</span> <span class="keyword">if</span> (e.other(v) == v) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (selfLoops % <span class="number">2</span> == <span class="number">0</span>) list.add(e);</span><br><span class="line">                    selfLoops++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        StringBuilder s = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        s.append(vertexes + <span class="string">&quot; &quot;</span> + edges + NEWLINE);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertexes; v++) &#123;</span><br><span class="line">            s.append(v + <span class="string">&quot;: &quot;</span>);</span><br><span class="line">            <span class="keyword">for</span> (Edge e : adj[v]) &#123;</span><br><span class="line">                s.append(e + <span class="string">&quot;  &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            s.append(NEWLINE);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> s.toString();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateVertexes</span><span class="params">(<span class="keyword">int</span>... vertexes)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; vertexes.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (vertexes[i] &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(</span><br><span class="line">                        String.format(<span class="string">&quot;Vertex %d must be a nonnegative integer.&quot;</span>, vertexes[i]));</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateEdges</span><span class="params">(<span class="keyword">int</span> edges)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (edges &lt; <span class="number">0</span>) <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;Number of edges must be nonnegative.&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// throw an IllegalArgumentException unless &#123;@code 0 &lt;= v &lt; V&#125;</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">validateVertex</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (v &lt; <span class="number">0</span> || v &gt;= vertexes)</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">&quot;vertex &quot;</span> + v + <span class="string">&quot; is not between 0 and &quot;</span> + (vertexes - <span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述代码是对<code>无向图</code>的扩展,它将<code>邻接表</code>中的元素从<code>整数</code>变为了<code>Edge</code>,函数<code>edges()</code>返回了<code>边</code>的集合,由于是<code>无向图</code>所以每条<code>边</code>会出现两次,需要注意处理.</p>
<p><code>加权无向图</code>的实现还拥有以下特点: </p>
<ul>
<li>边的比较: <code>Edge</code>类实现了<code>Comparable</code>接口,它使用了<code>权重</code>来比较两条<code>边</code>的大小,所以<code>加权无向图</code>的自然次序就是权重次序.</li>
</ul>
<ul>
<li>自环: 该实现允许存在自环,并且<code>edges()</code>函数中对自环边进行了记录.</li>
</ul>
<ul>
<li>平行边: 该实现允许存在平行边,但可以用更复杂的方法来消除平行边,例如只保留平行边中的权重最小者.</li>
</ul>
<h3 id="最小生成树"><a href="#最小生成树" class="headerlink" title="最小生成树"></a>最小生成树</h3><hr>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/mst.png" alt="加权无向图的最小生成树"></p>
<p><code>最小生成树</code>是<code>加权无向图</code>的重要应用.<strong><code>图</code>的<code>生成树</code>是它的一棵含有其所有<code>顶点</code>的<code>无环连通子图</code>,<code>最小生成树</code>是它的一棵<code>权值</code>(所有边的权值之和)最小的<code>生成树</code>.</strong></p>
<p>在给定的一幅<code>加权无向图</code>$G = (V,E)$中,$(u,v)$代表连接<code>顶点u</code>与<code>顶点v</code>的<code>边</code>,也就是$(u,v) \in E$,而$w(u,v)$代表这条边的<code>权重</code>,若存在<code>T</code>为<code>E</code>的子集,也就是$T \subseteq E$,且为<code>无环图</code>,使得$w(T) = \sum_{(u,v) \in T}w(u,v)$ 的 $w(T)$ 最小,则<code>T</code>为<code>G</code>的<code>最小生成树</code>.</p>
<p><code>最小生成树</code>在一些情况下可能会存在多个,例如,给定一幅图<code>G</code>,当它的所有边的<code>权重</code>都相同时,那么<code>G</code>的所有<code>生成树</code>都是<code>最小生成树</code>,当所有边的<code>权重</code>互不相同时,将会只有一个<code>最小生成树</code>.</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Multiple_minimum_spanning_trees.svg/316px-Multiple_minimum_spanning_trees.svg.png" alt="多个最小生成树的情况"></p>
<h3 id="切分定理"><a href="#切分定理" class="headerlink" title="切分定理"></a>切分定理</h3><hr>
<p><strong><code>切分定理</code>将图中的所有<code>顶点</code>切分为两个集合(两个非空且不重叠的集合),检查两个集合的所有边并识别哪条边应属于图的<code>最小生成树</code>.</strong></p>
<p>一种比较简单的切分方法即通过<strong>指定一个顶点集并隐式地认为它的补集为另一个顶点集来指定一个切分.</strong></p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/cut-property.png" alt="白色与灰色顶点代表了不同的顶点集"></p>
<p><code>切分定理</code>也表明了对于每一种切分,<code>权重</code>最小的<code>横切边(一条连接两个属于不同集合的顶点的边)</code>必然属于<code>最小生成树</code>.</p>
<p><code>切分定理</code>是解决<code>最小生成树</code>问题的所有算法的基础,<strong>使用<code>切分定理</code>找到<code>最小生成树</code>的一条边,不断重复直到找到<code>最小生成树</code>的所有边.</strong></p>
<p>这些算法可以说都是<code>贪心算法</code>,算法的每一步都是在找最优解(<code>权值</code>最小的<code>横切边</code>),而<strong>解决<code>最小生成树</code>的各种算法不同之处仅在于保存切分和判定<code>权重</code>最小的<code>横切边</code>的方式.</strong></p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/mst-greedy.png" alt="生成最小生成树的过程,权值最小的横切边将会被标记为黑色"></p>
<h3 id="Prim算法"><a href="#Prim算法" class="headerlink" title="Prim算法"></a>Prim算法</h3><hr>
<p><code>Prim算法</code>是用于解决<code>最小生成树</code>的算法之一,算法的每一步都会为一棵生长中的<code>树</code>添加一条边.一开始这棵树只有一个<code>顶点</code>,然后会一直添加到$V - 1$条边,<strong>每次总是将下一条连接<code>树</code>中的<code>顶点</code>与不在<code>树</code>中的<code>顶点</code>且<code>权重</code>最小的边加入到<code>树</code>中(也就是由<code>树</code>中<code>顶点</code>所定义的切分中的一条<code>横切边</code>).</strong></p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/prim.png"></p>
<p>实现<code>Prim算法</code>还需要借助以下数据结构: </p>
<ul>
<li>布尔值数组: 用于记录<code>顶点</code>是否已在<code>树</code>中.</li>
</ul>
<ul>
<li>队列: 使用一条队列来保存<code>最小生成树</code>中的边,也可以使用一个由<code>顶点</code>索引的<code>Edge</code>对象的数组.</li>
</ul>
<ul>
<li>优先队列: 优先队列用于保存<code>横切边</code>,优先队列的性质可以每次取出<code>权值</code>最小的<code>横切边</code>.</li>
</ul>
<h4 id="延时实现"><a href="#延时实现" class="headerlink" title="延时实现"></a>延时实现</h4><hr>
<p>当我们连接新加入<code>树</code>中的<code>顶点</code>与其他已经在<code>树</code>中<code>顶点</code>的所有边都失效了(由于两个<code>顶点</code>都已在<code>树</code>中,所以这是一条失效的<code>横切边</code>).我们需要处理这种情况,<strong>即使实现对无效边采取忽略(不加入到优先队列中),而延时实现会把无效边留在优先队列中,等到要删除优先队列中的数据时再进行有效性检查.</strong></p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/prim-lazy.png" alt="Prim延时实现的运行轨迹"></p>
<p>上图为<code>Prim算法</code>延时实现的轨迹图,它的步骤如下: </p>
<ul>
<li>将<code>顶点0</code>添加到<code>最小生成树</code>中,将它的<code>邻接表</code>中的所有边添加到优先队列中(将<code>横切边</code>添加到优先队列).</li>
</ul>
<ul>
<li>将<code>顶点7</code>和边<code>0-7</code>添加到<code>最小生成树</code>中,将<code>顶点</code>的<code>邻接表</code>中的所有边添加到优先队列中.</li>
</ul>
<ul>
<li>将<code>顶点1</code>和边<code>1-7</code>添加到<code>最小生成树</code>中,将<code>顶点</code>的<code>邻接表</code>中的所有边添加到优先队列中.</li>
</ul>
<ul>
<li>将<code>顶点2</code>和边<code>0-2</code>添加到<code>最小生成树</code>中,将边<code>2-3</code>和<code>6-2</code>添加到优先队列中,边<code>2-7</code>和<code>1-2</code>失效.</li>
</ul>
<ul>
<li>将<code>顶点3</code>和边<code>2-3</code>添加到<code>最小生成树</code>中,将边<code>3-6</code>添加到优先队列之中,边<code>1-3</code>失效.</li>
</ul>
<ul>
<li>将<code>顶点5</code>和边<code>5-7</code>添加到<code>最小生成树</code>中,将边<code>4-5</code>添加到优先队列中,边<code>1-5</code>失效.</li>
</ul>
<ul>
<li>从优先队列中删除失效边<code>1-3</code>,<code>1-5</code>,<code>2-7</code>.</li>
</ul>
<ul>
<li>将<code>顶点4</code>和边<code>4-5</code>添加到<code>最小生成树</code>中,将边<code>6-4</code>添加到优先队列中,边<code>4-7</code>,<code>0-4</code>失效.</li>
</ul>
<ul>
<li>从优先队列中删除失效边<code>1-2</code>,<code>4-7</code>,<code>0-4</code>.</li>
</ul>
<ul>
<li>将<code>顶点6</code>和边<code>6-2</code>添加到<code>最小生成树</code>中,和<code>顶点6</code>关联的其他边失效.</li>
</ul>
<ul>
<li>在添加<code>V</code>个顶点与<code>V - 1</code>条边之后,<code>最小生成树</code>就构造完成了,优先队列中剩余的边都为失效边.</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LazyPrimMST</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> EdgeWeightedGraph graph;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 记录最小生成树的总权重</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> weight;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 存储最小生成树的边</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Queue&lt;Edge&gt; mst;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 标记这个顶点在树中</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">boolean</span>[] marked;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 存储横切边的优先队列</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> PriorityQueue&lt;Edge&gt; pq;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">LazyPrimMST</span><span class="params">(EdgeWeightedGraph graph)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.graph = graph;</span><br><span class="line">        <span class="keyword">int</span> vertex = graph.vertex();</span><br><span class="line">        mst = <span class="keyword">new</span> ArrayDeque&lt;&gt;();</span><br><span class="line">        pq = <span class="keyword">new</span> PriorityQueue&lt;&gt;();</span><br><span class="line">        marked = <span class="keyword">new</span> <span class="keyword">boolean</span>[vertex];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            <span class="keyword">if</span> (!marked[v]) prim(v);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">prim</span><span class="params">(<span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">        scanAndPushPQ(s);</span><br><span class="line">        <span class="keyword">while</span> (!pq.isEmpty()) &#123;</span><br><span class="line">            Edge edge = pq.poll();  <span class="comment">// 取出权重最小的横切边</span></span><br><span class="line">            <span class="keyword">int</span> v = edge.either(), w = edge.other(v);  </span><br><span class="line">            <span class="keyword">assert</span> marked[v] || marked[w];</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (marked[v] &amp;&amp; marked[w])</span><br><span class="line">                <span class="keyword">continue</span>; <span class="comment">// 忽略失效边</span></span><br><span class="line"></span><br><span class="line">            mst.add(edge); <span class="comment">// 添加边到最小生成树中</span></span><br><span class="line">            weight += edge.weight(); <span class="comment">// 更新总权重</span></span><br><span class="line">			<span class="comment">// 继续将非树顶点加入到树中并更新横切边</span></span><br><span class="line">            <span class="keyword">if</span> (!marked[v]) scanAndPushPQ(v); </span><br><span class="line">            <span class="keyword">if</span> (!marked[w]) scanAndPushPQ(w); </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 标记顶点到树中,并且添加横切边到优先队列</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">scanAndPushPQ</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">assert</span> !marked[v];</span><br><span class="line">        marked[v] = <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">for</span> (Edge e : graph.adj(v))</span><br><span class="line">            <span class="keyword">if</span> (!marked[e.other(v)]) pq.add(e);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;Edge&gt; <span class="title">edges</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mst;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">weight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="即时实现"><a href="#即时实现" class="headerlink" title="即时实现"></a>即时实现</h4><hr>
<p>在即时实现中,将<code>v</code>添加到树中时,对于每个<code>非树顶点w</code>,<strong>不需要在优先队列中保存所有从<code>w</code>到<code>树顶点</code>的边,而只需要保存其中<code>权重</code>最小的边,所以在将<code>v</code>添加到<code>树</code>中后,要检查是否需要更新这条<code>权重</code>最小的边(如果<code>v-w</code>的<code>权重</code>更小的话).</strong></p>
<p>也可以认为只会在优先队列中保存每个<code>非树顶点w</code>的一条边(也是<code>权重</code>最小的那条边),将<code>w</code>和<code>树顶点</code>连接起来的其他<code>权重</code>较大的边迟早都会失效,所以没必要在优先队列中保存它们.</p>
<p>要实现即时版的<code>Prim算法</code>,需要使用两个顶点索引的数组<code>edgeTo[]</code>和<code>distTo[]</code>与一个索引优先队列,它们具有以下性质: </p>
<ul>
<li>如果<code>顶点v</code>不在树中但至少含有一条边和树相连,那么<code>edgeTo[v]</code>是将<code>v</code>和树连接的最短边,<code>distTo[v]</code>为这条边的<code>权重</code>.</li>
</ul>
<ul>
<li>所有这类<code>顶点v</code>都保存在索引优先队列中,索引<code>v</code>关联的值是<code>edgeTo[v]</code>的边的<code>权重</code>.</li>
</ul>
<ul>
<li>索引优先队列中的最小键即是<code>权重</code>最小的<code>横切边</code>的<code>权重</code>,而和它相关联的顶点<code>v</code>就是下一个将要被添加到<code>树</code>中的<code>顶点</code>.</li>
</ul>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/prim-eager.png" alt="即时实现Prim算法的运行轨迹"></p>
<ul>
<li>将<code>顶点0</code>添加到<code>最小生成树</code>之中,将它的<code>邻接表</code>中的所有边添加到优先队列中(这些边是目前唯一已知的横切边).</li>
</ul>
<ul>
<li>将<code>顶点7</code>和边<code>0-7</code>添加到<code>最小生成树</code>,将边<code>1-7</code>和<code>5-7</code>添加到优先队列中,将连接<code>顶点4</code>与树的最小边由<code>0-4</code>替换为<code>4-7</code>.</li>
</ul>
<ul>
<li>将<code>顶点1</code>和边<code>1-7</code>添加到<code>最小生成树</code>,将边<code>1-3</code>添加到优先队列.</li>
</ul>
<ul>
<li>将<code>顶点2</code>和边<code>0-2</code>添加到最小生成树,将连接<code>顶点6</code>与树的最小边由<code>0-6</code>替换为<code>6-2</code>,将连接<code>顶点3</code>与树的最小边由<code>1-3</code>替换为<code>2-3</code>.</li>
</ul>
<ul>
<li>将<code>顶点3</code>和边<code>2-3</code>添加到<code>最小生成树</code>.</li>
</ul>
<ul>
<li>将<code>顶点5</code>和边<code>5-7</code>添加到<code>最小生成树</code>,将连接<code>顶点4</code>与树的最小边<code>4-7</code>替换为<code>4-5</code>.</li>
</ul>
<ul>
<li>将<code>顶点4</code>和边<code>4-5</code>添加到<code>最小生成树</code>.</li>
</ul>
<ul>
<li>将<code>顶点6</code>和边<code>6-2</code>添加到<code>最小生成树</code>.</li>
</ul>
<ul>
<li>在添加了<code>V - 1</code>条边之后,<code>最小生成树</code>构造完成并且优先队列为空.</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PrimMST</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> EdgeWeightedGraph graph;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 存放最小生成树中的边</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Edge[] edgeTo;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 每条边对应的权重</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">double</span>[] distTo;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">boolean</span>[] marked;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> IndexMinPQ&lt;Double&gt; pq;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PrimMST</span><span class="params">(EdgeWeightedGraph graph)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.graph = graph;</span><br><span class="line">        <span class="keyword">int</span> vertex = graph.vertex();</span><br><span class="line">        <span class="keyword">this</span>.edgeTo = <span class="keyword">new</span> Edge[vertex];</span><br><span class="line">        <span class="keyword">this</span>.marked = <span class="keyword">new</span> <span class="keyword">boolean</span>[vertex];</span><br><span class="line">        <span class="keyword">this</span>.pq = <span class="keyword">new</span> IndexMinPQ&lt;&gt;(vertex);</span><br><span class="line">        <span class="keyword">this</span>.distTo = <span class="keyword">new</span> <span class="keyword">double</span>[vertex];</span><br><span class="line">		<span class="comment">// 将权重数组初始化为无穷大</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; vertex; i++)</span><br><span class="line">            distTo[i] = Double.POSITIVE_INFINITY;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; vertex; v++)</span><br><span class="line">            <span class="keyword">if</span> (!marked[v]) prim(v);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">prim</span><span class="params">(<span class="keyword">int</span> s)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 将起点设为0.0并加入到优先队列</span></span><br><span class="line">        distTo[s] = <span class="number">0.0</span>;</span><br><span class="line">        pq.insert(s, distTo[s]);</span><br><span class="line">        <span class="keyword">while</span> (!pq.isEmpty()) &#123;</span><br><span class="line">			<span class="comment">// 取出权重最小的边,优先队列中存的顶点是与树相连的非树顶点,</span></span><br><span class="line">			<span class="comment">// 同时它也是下一次要加入到树中的顶点</span></span><br><span class="line">            <span class="keyword">int</span> v = pq.delMin();</span><br><span class="line">            scan(v);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">scan</span><span class="params">(<span class="keyword">int</span> v)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// 将顶点加入到树中</span></span><br><span class="line">        marked[v] = <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (Edge e : graph.adj(v)) &#123;</span><br><span class="line">            <span class="keyword">int</span> w = e.other(v);</span><br><span class="line">			<span class="comment">// 忽略失效边</span></span><br><span class="line">            <span class="keyword">if</span> (marked[w]) <span class="keyword">continue</span>;</span><br><span class="line">			<span class="comment">// 如果w与连接树顶点的边的权重小于其他w连接树顶点的边</span></span><br><span class="line">			<span class="comment">// 则进行替换更新</span></span><br><span class="line">            <span class="keyword">if</span> (e.weight() &lt; distTo[w]) &#123;</span><br><span class="line">                distTo[w] = e.weight();</span><br><span class="line">                edgeTo[w] = e;</span><br><span class="line">                <span class="keyword">if</span> (pq.contains(w))</span><br><span class="line">                    pq.decreaseKey(w, distTo[w]);</span><br><span class="line">                <span class="keyword">else</span></span><br><span class="line">                    pq.insert(w, distTo[w]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;Edge&gt; <span class="title">edges</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Queue&lt;Edge&gt; mst = <span class="keyword">new</span> ArrayDeque&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> v = <span class="number">0</span>; v &lt; edgeTo.length; v++) &#123;</span><br><span class="line">            Edge e = edgeTo[v];</span><br><span class="line">            <span class="keyword">if</span> (e != <span class="keyword">null</span>) &#123;</span><br><span class="line">                mst.add(e);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> mst;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">weight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">double</span> weight = <span class="number">0.0</span>;</span><br><span class="line">        <span class="keyword">for</span> (Edge e : edges())</span><br><span class="line">            weight += e.weight();</span><br><span class="line">        <span class="keyword">return</span> weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>不管是<code>延迟实现</code>还是<code>即时实现</code>,<code>Prim算法</code>的规律就是: <strong>在<code>树</code>的生长过程中,都是通过连接一个和新加入的<code>顶点</code>相邻的<code>顶点</code>.当新加入的<code>顶点</code>周围没有<code>非树顶点</code>时,树的生长又会从另一部分开始.</strong></p>
<h3 id="Kruskal算法"><a href="#Kruskal算法" class="headerlink" title="Kruskal算法"></a>Kruskal算法</h3><hr>
<p><code>Kruskal算法</code>的思想是<strong>按照边的<code>权重</code>顺序由小到大处理它们</strong>,将边添加到<code>最小生成树</code>,加入的边不会与已经在<code>树</code>中的边构成环,直到<code>树</code>中含有<code>V - 1</code>条边为止.<strong>这些边会逐渐由一片<code>森林</code>合并为一棵<code>树</code></strong>,也就是我们需要的<code>最小生成树</code>.</p>
<p><img src="http://algs4.cs.princeton.edu/43mst/images/kruskal.png" alt="Kruskal算法的运行轨迹"></p>
<h4 id="与Prim算法的区别"><a href="#与Prim算法的区别" class="headerlink" title="与Prim算法的区别"></a>与Prim算法的区别</h4><hr>
<ul>
<li><code>Prim算法</code>是一条边一条边地来构造<code>最小生成树</code>,每一步都会为<code>树</code>中添加一条边.</li>
</ul>
<ul>
<li><code>Kruskal算法</code>构造<code>最小生成树</code>也是一条边一条边地添加,但不同的是它寻找的边会连接一片<code>森林</code>中的两棵<code>树</code>.从一片由<code>V</code>棵单<code>顶点</code>的树构成的<code>森林</code>开始并不断地将两棵<code>树</code>合并(可以找到的最短边)直到只剩下一棵<code>树</code>,它就是<code>最小生成树</code>.</li>
</ul>
<h4 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h4><hr>
<p>要实现<code>Kruskal算法</code>需要借助<code>Union-Find</code>数据结构,它是一种树型的数据结构,用于处理一些不相交集合的合并与查询问题.</p>
<p>关于<code>Union-Find</code>的更多资料可以参考下面的链接: </p>
<ul>
<li><a target="_blank" rel="noopener" href="https://github.com/SylvanasSun/algs4-study/blob/15ae228a1bc6a75465a96681caaa93eff3462327/src/main/java/chapter1_fundamentals/C1_5_UnionFind/UF.java">Union-Find简单实现</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint-set data structure - Wikipedia</a></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">KruskalMST</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 这条队列用于记录最小生成树中的边集</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Queue&lt;Edge&gt; mst;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> weight;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">KruskalMST</span><span class="params">(EdgeWeightedGraph graph)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.mst = <span class="keyword">new</span> ArrayDeque&lt;&gt;();</span><br><span class="line">        <span class="comment">// 创建一个优先队列,并将图的所有边添加到优先队列中</span></span><br><span class="line">        PriorityQueue&lt;Edge&gt; pq = <span class="keyword">new</span> PriorityQueue&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (Edge e : graph.edges()) &#123;</span><br><span class="line">            pq.add(e);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> vertex = graph.vertex();</span><br><span class="line">		<span class="comment">// 创建一个Union-Find</span></span><br><span class="line">        UF uf = <span class="keyword">new</span> UF(vertex);</span><br><span class="line">		<span class="comment">// 一条一条地添加边到最小生成树,直到添加了 V - 1条边</span></span><br><span class="line">        <span class="keyword">while</span> (!pq.isEmpty() &amp;&amp; mst.size() &lt; vertex - <span class="number">1</span>) &#123;</span><br><span class="line">			<span class="comment">// 取出权重最小的边</span></span><br><span class="line">            Edge e = pq.poll();</span><br><span class="line">            <span class="keyword">int</span> v = e.either();</span><br><span class="line">            <span class="keyword">int</span> w = e.other(v);</span><br><span class="line">            <span class="comment">// 如果这条边的两个顶点不在一个分量中(对于union-find数据结构中而言)</span></span><br><span class="line">            <span class="keyword">if</span> (!uf.connected(v, w)) &#123;</span><br><span class="line">				<span class="comment">// 将v和w归并(对于union-find数据结构中而言),然后将边添加进树中,并计算更新权重</span></span><br><span class="line">                uf.union(v, w); </span><br><span class="line">                mst.add(e);</span><br><span class="line">                weight += e.weight();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Iterable&lt;Edge&gt; <span class="title">edges</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mst;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">weight</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> weight;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面代码实现的<code>Kruskal算法</code>使用了一条队列来保存<code>最小生成树</code>的边集,一条优先队列来保存还未检查的边,一个<code>Union-Find</code>来判断失效边.</p>
<h4 id="性能比较"><a href="#性能比较" class="headerlink" title="性能比较"></a>性能比较</h4><hr>
<table>
<thead>
<tr>
<th>算法</th>
<th>空间复杂度</th>
<th>时间复杂度</th>
</tr>
</thead>
<tbody><tr>
<td>Prim(延时)</td>
<td>E</td>
<td>ElogE</td>
</tr>
<tr>
<td>Prim(即时)</td>
<td>V</td>
<td>ElogV</td>
</tr>
<tr>
<td>Kruskal</td>
<td>E</td>
<td>ElogE</td>
</tr>
</tbody></table>
<h3 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h3><hr>
<ul>
<li><a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Minimum_spanning_tree">Minimum spanning tree - Wikipedia</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="http://algs4.cs.princeton.edu/43mst/">Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne</a></li>
</ul>
<h3 id="图的那点事儿"><a href="#图的那点事儿" class="headerlink" title="图的那点事儿"></a>图的那点事儿</h3><hr>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/18/2017-07-18-Graph_UndirectedGraph/">图的那点事儿(1)-无向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/23/2017-07-23-Graph_DirectedGraphs/">图的那点事儿(2)-有向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/25/2017-07-25-Graph_WeightedUndirectedGraph/">图的那点事儿(3)-加权无向图</a></li>
</ul>
<ul>
<li><a target="_blank" rel="noopener" href="https://sylvanassun.github.io/2017/07/27/2017-07-27-Graph_WeightedDigraph">图的那点事儿(4)-加权有向图</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/2017/" rel="tag"># 2017</a>
              <a href="/yuwanzi.io/tags/Algorithms/" rel="tag"># Algorithms</a>
              <a href="/yuwanzi.io/tags/Graph/" rel="tag"># Graph</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2017/07/23/2017-07-23-Graph_DirectedGraphs/" rel="prev" title="图的那点事儿(2)-有向图">
                  <i class="fa fa-chevron-left"></i> 图的那点事儿(2)-有向图
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2017/07/27/2017-07-27-Graph_WeightedDigraph/" rel="next" title="图的那点事儿(4)-加权有向图">
                  图的那点事儿(4)-加权有向图 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
